package com.example.servicetest

import android.content.Context
import android.graphics.*
import android.os.Build
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import androidx.core.content.ContextCompat


class PolygonalLayout:FrameLayout {

    private var mPolygonalWidth = 0

    private var mPolygonalHeight = 0

    private lateinit var colors:IntArray

    private lateinit var mPaint: Paint

    private lateinit var mClipPath: Path

    private lateinit var mAreaRegion: Region

    constructor(context: Context) : this(context,null)

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs,0)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ){
        initialize(attrs)
    }

    private fun initialize(attrs: AttributeSet?){
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        val array = context.obtainStyledAttributes(attrs, R.styleable.PolygonalLayout)

        mPaint = Paint().apply {
            isAntiAlias = true
            style = Paint.Style.FILL
            color = ContextCompat.getColor(context,R.color.colorD520F4)
//            xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_IN)
        }

        colors = intArrayOf(
            ContextCompat.getColor(context,R.color.color002871),
            ContextCompat.getColor(context,R.color.color000000),
            ContextCompat.getColor(context,R.color.colorD520F4),
            ContextCompat.getColor(context,R.color.color13D2F2),
        )

        attrs?.run {
            mPolygonalWidth = array.getDimension(R.styleable.PolygonalLayout_polygonal_width, 0f).toInt()
            mPolygonalHeight = array.getDimension(R.styleable.PolygonalLayout_polygonal_height, 0f).toInt()
        }

        mClipPath = Path()
        mAreaRegion = Region()

        array.recycle()
    }

    override fun dispatchDraw(canvas: Canvas) {
        super.dispatchDraw(canvas)

        canvas.saveLayer(
            RectF(0f, 0f, canvas.width.toFloat(), canvas.height.toFloat()),
            mPaint,
        )
        // 绘制子控件
        super.dispatchDraw(canvas)
        // 绘制带有圆角的 Path
//        mPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_IN);
//        mPaint.strokeWidth = 0f
//        mPaint.style = Paint.Style.FILL
//        canvas.drawPath(mClipPath, mPaint)
//        canvas.restore()

        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O_MR1) {
            mPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_IN)
            canvas.drawPath(mClipPath, mPaint)
        } else {
            mPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_IN)
            val path = Path()
            path.addRect(
                0f,
                0f,
                canvas.width.toFloat(),
                canvas.height.toFloat(),
                Path.Direction.CW
            )
            path.op(mClipPath, Path.Op.DIFFERENCE)
            canvas.drawPath(path, mPaint)
        }
        canvas.restore()
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        val areas = RectF()

        areas.left = paddingLeft.toFloat()
        areas.top = paddingTop.toFloat()
        areas.right = (w - paddingRight).toFloat();
        areas.bottom = (h - paddingBottom).toFloat();

        val d: Float = if (areas.width() >= areas.height()) areas.height() else areas.width()
        val r = d / 2

        val center = PointF((w / 2).toFloat(), (h / 2).toFloat())
        mClipPath.addCircle(center.x, center.y, r, Path.Direction.CW)
        mClipPath.moveTo(0f, 0f) // 通过空操作让Path区域占满画布
        mClipPath.moveTo(w.toFloat(), h.toFloat())

        val clip = Region(areas.left.toInt(), areas.top.toInt(), areas.right.toInt(), areas.bottom.toInt())
        mAreaRegion.setPath(mClipPath, clip)

    }


}